﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace DALL.SysManage
{
    public class T_PUB_BRANCH
    {
        public T_PUB_BRANCH()
        { }
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.SysManage.T_PUB_BRANCH model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_BRANCH(");
            strSql.Append("BRANCH_NAME,ADDRESS,PHONE,NAS_IP,HEAD,IS_BRANCH,NAS_USERID,NAS_PASSWORD,IS_SYNCHRONIZED)");

            strSql.Append(" values (");
            strSql.Append("@BRANCH_NAME,@ADDRESS,@PHONE,@NAS_IP,@HEAD,@IS_BRANCH,@NAS_USERID,@NAS_PASSWORD,@IS_SYNCHRONIZED)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BRANCH_NAME", DbType.String, model.BRANCH_NAME);
            db.AddInParameter(dbCommand, "ADDRESS", DbType.String, model.ADDRESS);
            db.AddInParameter(dbCommand, "PHONE", DbType.String, model.PHONE);
            db.AddInParameter(dbCommand, "NAS_IP", DbType.String, model.NAS_IP);
            db.AddInParameter(dbCommand, "HEAD", DbType.String, model.HEAD);
            db.AddInParameter(dbCommand, "IS_BRANCH", DbType.Boolean, model.IS_BRANCH);
            db.AddInParameter(dbCommand, "NAS_USERID", DbType.String, model.NAS_USERID);
            db.AddInParameter(dbCommand, "NAS_PASSWORD", DbType.String, model.NAS_PASSWORD);
            db.AddInParameter(dbCommand, "IS_SYNCHRONIZED", DbType.Boolean, model.IS_SYNCHRONIZED);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
       /// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(Model.SysManage.T_PUB_BRANCH model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_BRANCH set ");
            strSql.Append("BRANCH_NAME=@BRANCH_NAME,");
            strSql.Append("ADDRESS=@ADDRESS,");
            strSql.Append("PHONE=@PHONE,");
            strSql.Append("NAS_IP=@NAS_IP,");
            strSql.Append("HEAD=@HEAD,");
            strSql.Append("IS_BRANCH=@IS_BRANCH,");
            strSql.Append("NAS_USERID=@NAS_USERID,");
            strSql.Append("NAS_PASSWORD=@NAS_PASSWORD,");
            strSql.Append("IS_SYNCHRONIZED=@IS_SYNCHRONIZED");
            strSql.Append(" where BRANCH_ID=@BRANCH_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "BRANCH_NAME", DbType.String, model.BRANCH_NAME);
            db.AddInParameter(dbCommand, "ADDRESS", DbType.String, model.ADDRESS);
            db.AddInParameter(dbCommand, "PHONE", DbType.String, model.PHONE);
            db.AddInParameter(dbCommand, "NAS_IP", DbType.String, model.NAS_IP);
            db.AddInParameter(dbCommand, "HEAD", DbType.String, model.HEAD);
            db.AddInParameter(dbCommand, "IS_BRANCH", DbType.Boolean, model.IS_BRANCH);
            db.AddInParameter(dbCommand, "NAS_USERID", DbType.String, model.NAS_USERID);
            db.AddInParameter(dbCommand, "NAS_PASSWORD", DbType.String, model.NAS_PASSWORD);
            db.AddInParameter(dbCommand, "IS_SYNCHRONIZED", DbType.Boolean, model.IS_SYNCHRONIZED);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int BRANCH_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_BRANCH ");
            strSql.Append(" where BRANCH_ID=@BRANCH_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, BRANCH_ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

       /// <summary>
		/// 得到一个对象实体
		/// </summary>
        public Model.SysManage.T_PUB_BRANCH GetModel(int BRANCH_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BRANCH_ID,BRANCH_NAME,ADDRESS,PHONE,NAS_IP,HEAD,IS_BRANCH,NAS_USERID,NAS_PASSWORD,IS_SYNCHRONIZED from T_PUB_BRANCH ");
            strSql.Append(" where BRANCH_ID=@BRANCH_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, BRANCH_ID);
            Model.SysManage.T_PUB_BRANCH model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BRANCH_ID,BRANCH_NAME,ADDRESS,PHONE,NAS_IP,HEAD,IS_BRANCH,NAS_USERID,NAS_PASSWORD,IS_SYNCHRONIZED ");
            strSql.Append(" FROM T_PUB_BRANCH ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
		/// 获得数据列表（比DataSet效率高，推荐使用）
		/// </summary>
        public List<Model.SysManage.T_PUB_BRANCH> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BRANCH_ID,BRANCH_NAME,ADDRESS,PHONE,NAS_IP,HEAD,IS_BRANCH,NAS_USERID,NAS_PASSWORD,IS_SYNCHRONIZED ");
            strSql.Append(" FROM T_PUB_BRANCH ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.SysManage.T_PUB_BRANCH> list = new List<Model.SysManage.T_PUB_BRANCH>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


       /// <summary>
		/// 对象实体绑定数据
		/// </summary>
        public Model.SysManage.T_PUB_BRANCH ReaderBind(IDataReader dataReader)
        {
            Model.SysManage.T_PUB_BRANCH model = new Model.SysManage.T_PUB_BRANCH();
            object ojb;
            ojb = dataReader["BRANCH_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BRANCH_ID = (int)ojb;
            }
            model.BRANCH_NAME = dataReader["BRANCH_NAME"].ToString();
            model.ADDRESS = dataReader["ADDRESS"].ToString();
            model.PHONE = dataReader["PHONE"].ToString();
            model.NAS_IP = dataReader["NAS_IP"].ToString();
            model.HEAD = dataReader["HEAD"].ToString();
            ojb = dataReader["IS_BRANCH"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IS_BRANCH = (bool)ojb;
            }
            model.NAS_USERID = dataReader["NAS_USERID"].ToString();
            model.NAS_PASSWORD = dataReader["NAS_PASSWORD"].ToString();
            ojb = dataReader["IS_SYNCHRONIZED"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IS_SYNCHRONIZED = (bool)ojb;
            }
            return model;
        }
        #endregion  Method
    }
}
